package top.tenebrous.yzl.handler.sechub;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import top.tenebrous.yzl.basic.constant.Constants;
import top.tenebrous.yzl.chain.AbstractJesticChainHandler;
import top.tenebrous.yzl.context.JesticContext;
import top.tenebrous.yzl.context.JesticTask;
import top.tenebrous.yzl.context.Response;
import top.tenebrous.yzl.entity.JesticServer;
import top.tenebrous.yzl.enums.FailCodeEnum;
import top.tenebrous.yzl.instance.AbstractInstanceContext;
import top.tenebrous.yzl.instance.Instance;
import top.tenebrous.yzl.model.request.JestcSechubRequst;
import top.tenebrous.yzl.model.response.JestcSechubResponse;
import top.tenebrous.yzl.service.IAuthorityService;

import java.util.Objects;

/**
 * @author yazhang6
 */
@Slf4j
public class JesticSechubCheckHandler extends AbstractJesticChainHandler<JestcSechubRequst, JestcSechubResponse> {

    int order;

    private final AbstractInstanceContext abstractInstanceContext;

    private final IAuthorityService authorityService;

    public JesticSechubCheckHandler(AbstractInstanceContext abstractInstanceContext, IAuthorityService authorityService) {
        this.abstractInstanceContext = abstractInstanceContext;
        this.authorityService = authorityService;
    }

    @Override
    public void handler(JesticContext<? extends JesticTask, ? extends Response> context) {
        JestcSechubRequst request = (JestcSechubRequst) context.getRequest();
        JestcSechubResponse response = (JestcSechubResponse) context.getResponse();
        log.info("JesticSechubCheckHandler.handler.start:[{}]", JSON.toJSONString(request));
        request.start();
        if (StringUtils.isBlank(request.getServerName()) || StringUtils.isBlank(request.getServerId())) {
            response.buildResponseError(FailCodeEnum.SERVER_PARAM_NOT_FOUND);
            request.end();
            return;
        }

        // 验证token
        Instance instance = abstractInstanceContext.getInstanceByToken(request.getToken());
        if (Objects.isNull(instance)) {
            response.buildResponseError(FailCodeEnum.SERVER_TOKEN_NOT_FOUND);
            request.end();
            return;
        }

        // 查询服务
        JesticServer jesticServer = authorityService.queryServer(request.getServerId(), request.getServerName());
        if (Objects.isNull(jesticServer)) {
            response.buildResponseError(FailCodeEnum.SERVER_NOT_FOUND);
            request.end();
            return;
        }
        if (jesticServer.getServerStatus() != Constants.INT_ONE) {
            response.buildResponseError(FailCodeEnum.SERVER_NOT_RUNNING);
            request.end();
            return;
        }
        context.setJesticServer(jesticServer);

        if (null != nextHandler) {
            nextHandler.handler(context);
        } else {
            request.end();
        }

    }

    @Override
    public int order(String taskType) {
        return this.order;
    }

    @Override
    public int getOrder() {
        return this.order;
    }

    @Override
    public AbstractJesticChainHandler<JestcSechubRequst, JestcSechubResponse> setOrder(int order) {
        this.order = order;
        return this;
    }
}
